package com.rritw.webtools.action.tool;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.rritw.webtools.action.IpsAction;
import com.rritw.webtools.common.Const;
import com.rritw.webtools.util.FileUtil;
import com.rritw.webtools.util.JSoapUtil;
import com.rritw.webtools.util.StringUtil;

@Controller
@Scope("prototype")
public class Csv2HtmlTableAction extends IpsAction {

    private static final long serialVersionUID = 7002985971708790168L;
    private String            content;
    private String            splitChar;
    private String            customSplitChar;
    private String            contentResult;

    public String index() {
        super.init();
        return SUCCESS;
    }

    public String create() {
        super.init();
        if (StringUtil.isEmpty(splitChar) && StringUtil.isEmpty(customSplitChar)) {
            return ERROR;
        }
        if (StringUtil.isEmpty(content)) {
            return ERROR;
        }
        String _splitChar = null;
        if (!StringUtil.isEmpty(customSplitChar)) {
            _splitChar = customSplitChar;
        } else if ("comma".equals(splitChar)) {
            _splitChar = ",";
        } else if ("tab".equals(splitChar)) {
            _splitChar = "\t";
        } else if ("space".equals(splitChar)) {
            _splitChar = " ";
        }

        contentResult = JSoapUtil.formatHtml(getTableHtml(content, _splitChar));

        return SUCCESS;
    }

    private static String getTableHtml(String _content, String _splitChar) {
        StringBuilder sb = new StringBuilder();
        sb.append("<html><body><table>");
        List<String> lines = StringUtil.getContentList(_content, Const.LINE_SPLITER);
        if (lines.size() > 0) {
            for (String line : lines) {
                sb.append("<tr>");
                List<String> fields = StringUtil.getContentList(line, _splitChar);
                for (String field : fields) {
                    sb.append("<td>");
                    sb.append(field);
                    sb.append("</td>");
                }
                sb.append("</tr>");
            }
        }
        sb.append("</table></body></html>");
        return sb.toString();
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getSplitChar() {
        return splitChar;
    }

    public void setCustomSplitChar(String customSplitChar) {
        this.customSplitChar = customSplitChar;
    }

    public String getCustomSplitChar() {
        return customSplitChar;
    }

    public void setSplitChar(String splitChar) {
        this.splitChar = splitChar;
    }

    public String getContentResult() {
        return contentResult;
    }

    public static void main(String[] args) throws IOException {
        String content = FileUtil.read(new File("e:\\temp\\test.csv"));
        content = JSoapUtil.formatHtml(getTableHtml(content, ","));
        System.out.println(content);
    }

}
